package com.fx.service.impl;

import cn.hutool.core.date.DateUtil;

import com.fx.entity.AdminTempUser;
import com.fx.entity.SysRes;
import com.fx.mapper.SysResMapper;
import com.fx.service.SysResService;
import com.fx.util.BuildTree;
import com.fx.util.IdWorker;
import com.fx.util.Tree;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: EDZ
 * @Date: 2018-5-18 11:55:11
 * @Email: 你的邮箱
 * 概要说明：该类干嘛用的
 **/
@Service
public class SysResServiceImpl implements SysResService {

    @Autowired
    IdWorker idWorker;
    @Autowired
    SysResMapper sysResMapper;

    /**
     * 方法名称：根据角色获取该角色所有资源
     * 概要说明：方法说明
     *
     * @param roleId
     * @Ather EDZ
     * @Date 2018-5-18 11:54
     * @Param [roleId] 角色id
     * @Return java.util.List<com.alibaba.fastjson.JSONObject>
     */
    @Override
    public List<Tree<SysRes>> getResListByRoleId(String roleId) {
        List<Tree<SysRes>> treeList = new ArrayList<Tree<SysRes>>();
        List<SysRes> resList = sysResMapper.getResListByRoleId(roleId);
        if (resList != null && resList.size() != 0) {
            for (int i = 0; i < resList.size(); i++) {
                SysRes res = resList.get(i);
                Tree<SysRes> tree = new Tree<SysRes>();
                tree.setId(res.getFlId());
                tree.setParentId(res.getFlParentId());
                tree.setText(res.getFlName());
                Map<String, Object> attributes = new HashMap<String, Object>(16);
                attributes.put("url", res.getFlUrl());
                attributes.put("icon", res.getFlIcon());
                tree.setAttributes(attributes);
                treeList.add(tree);
            }
        }
        List<Tree<SysRes>> temp = BuildTree.buildList(treeList, "0");
        return temp;
    }

    @Override
    public List<SysRes> listData(Map<String, Object> param) {
        return sysResMapper.queryListData(param);
    }

    @Override
    public int getCount(Map<String, Object> param) {
        return sysResMapper.queryCount(param);
    }

    @Override
    public SysRes getById(String flId) {
        return sysResMapper.queryById(flId);
    }

    @Override
    public int save(SysRes sysRes) {
        sysRes.setFlId(String.valueOf(idWorker.nextId()));
        sysRes.setFlCreatedTime(DateUtil.now());
        sysRes.setFlCreator(((AdminTempUser) SecurityUtils.getSubject().getPrincipal()).getFlName());
        return sysResMapper.save(sysRes);
    }

    @Override
    public int updateById(SysRes sysRes) {
        sysRes.setFlUpDatedTime(DateUtil.now());
        return sysResMapper.updateById(sysRes);
    }

    @Override
    public int batchDelByIds(String[] ids) {
        return sysResMapper.batchRemove(ids);
    }

    @Override
    public List<SysRes> getAll() {
        Map<String,Object> param = new HashMap<String,Object>();
        return sysResMapper.queryListData(param);
    }

    @Override
    public Tree<SysRes> getTree() {
        List<Tree<SysRes>> trees = new ArrayList<Tree<SysRes>>();
        Map<String, Object> map = new HashMap<>();
        map.put("fl_parent_id", "0");
        List<SysRes> list = sysResMapper.queryListData(map);
        for (SysRes res : list) {
            Tree<SysRes> tree = new Tree<SysRes>();
            tree.setId(res.getFlId());
            tree.setParentId("0");
            tree.setText(res.getFlName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<SysRes> t = BuildTree.build(trees);
        return t;
    }

    @Override
    public List<SysRes> getAllRes() {
        Map<String,Object> param = new HashMap<String,Object>();
        return sysResMapper.queryListData(param);
    }

}
